package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.platform.dto.BasicDeviceAttributeDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceAttributeEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceTypeEntity;
import com.iwomy.secureplat.platform.mapper.BasicDeviceAttributeMapper;
import com.iwomy.secureplat.platform.service.BasicDeviceAttributeService;
import com.iwomy.secureplat.platform.vo.BasicDeviceAttributeVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备属性
 *
 * @author pig
 * @date 2023-11-14 10:34:52
 */
@Service
public class BasicDeviceAttributeServiceImpl extends ServiceImpl<BasicDeviceAttributeMapper, BasicDeviceAttributeEntity> implements BasicDeviceAttributeService {
	@Override
	public Page<BasicDeviceAttributeVO> getBasicDeviceAttributePage(Page page, BasicDeviceAttributeDTO deviceAttributeDTO) {
		//多表查询
		MPJLambdaWrapper<BasicDeviceAttributeEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(BasicDeviceAttributeEntity.class)
				.selectAs(BasicDeviceTypeEntity::getTypeName, BasicDeviceAttributeVO::getDeviceType)
				.leftJoin(BasicDeviceTypeEntity.class, BasicDeviceTypeEntity::getId, BasicDeviceAttributeEntity::getDeviceTypeId)

		;

		if (ObjectUtil.isNotEmpty(deviceAttributeDTO.getDeviceTypeId())) {
			wrapper.eq(BasicDeviceAttributeEntity::getDeviceTypeId, deviceAttributeDTO.getDeviceTypeId());
		}
		if (StrUtil.isNotEmpty(deviceAttributeDTO.getAttributeName())) {
			wrapper.like(BasicDeviceAttributeEntity::getAttributeName, deviceAttributeDTO.getAttributeName());
		}
		if (ArrayUtil.isNotEmpty(deviceAttributeDTO.getCreateTime())) {
			wrapper.ge(BasicDeviceAttributeEntity::getCreateTime, deviceAttributeDTO.getCreateTime()[0]).le(BasicDeviceAttributeEntity::getCreateTime,
					deviceAttributeDTO.getCreateTime()[1]);
		}

		return this.getBaseMapper().selectJoinPage(page, BasicDeviceAttributeVO.class, wrapper);

	}
}